package game

import (
	"fmt"
	"github.com/robfig/cron/v3"
	"math/rand"
	"server/GoUtil"
	"server/MergeConst"
	"server/db"
	"server/gamedata"
	"server/msg"
	"strconv"
	"strings"
	"time"

	// "github.com/golang/protobuf/ptypes/timestamp"
	"google.golang.org/protobuf/proto"
)

type PlayerShopData struct {
	*PlayerData
	Data               msg.ResShopData
	MShopTimeRenew     int32
	MShopPackRenewData map[int32]int32
	EnergyRenewTime    int32
	DailyRenewTime     int32
	BuyEnergyTime      int32
	MsqlStruck         db.SqlShopInfoStruct
	ZeroCronID         cron.EntryID
	TwelveCronID       cron.EntryID
	WeekCronID         cron.EntryID
	Mcron              *cron.Cron
}

func (p *PlayerShopData) LoadDataFromDB(dwUin interface{}) bool {

	sqlStr := "SELECT * FROM t_player_shop_data WHERE dwUin = ?"

	p.Data.MShopTimeBuyData = make(map[int32]int32)
	p.Data.MShopSaleBuyData = make(map[int32]int32)
	p.Data.MPackBuyData = make(map[int32]int32)
	p.Data.MSpecialOfferBuyData = make(map[int32]int32)
	p.Data.MUISpecialOfferBuyData = make(map[int32]int32)
	p.Data.MFreePackBuyData = make(map[int32]int32)
	p.Data.MDiamondFirstBuyData = make(map[int32]int32)
	p.MShopPackRenewData = make(map[int32]int32)

	if err := db.SqlDb.Get(&p.MsqlStruck, sqlStr, dwUin.(int32)); err != nil {
		fmt.Printf("get data failed, err:%v\n", err)
		p.IsHaveDataDb = false
	} else {
		p.IsHaveDataDb = true
	}
	ShopItems := gamedata.GetConfigByName("ShopItem")
	PackItems := gamedata.GetConfigByName("ShopPack")
	timeStamp := time.Now().Unix()
	t := time.Unix(timeStamp, 0).Local()
	p.BuyEnergyTime = p.MsqlStruck.BuyEnergyTime
	if p.IsHaveDataDb == false {

		for i := 0; i < ShopItems.NumRecord(); i++ {
			item := ShopItems.Record(i).(*gamedata.ShopItemRecord)
			if item.Group == "Time" {
				p.Data.MShopTimeBuyData[int32(item.Id)] = int32(item.TotalCount)
			}
			// if item.Group == "Sale" {
			// 	p.Data.MShopSaleBuyData[int32(item.Id)] = int32(item.TotalCount)
			// }

		}
		var renewTime1 int64 = 0
		if t.Hour() >= 12 {
			renewTime1 = timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()) + 12*3600
		} else {
			renewTime1 = timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second())
		}
		p.MShopTimeRenew = int32(renewTime1)

		for i := 0; i < PackItems.NumRecord(); i++ {
			item := PackItems.Record(i).(*gamedata.ShopPackRecord)
			p.Data.MPackBuyData[int32(item.Id)] = int32(item.TotalCount)
			if item.Id == 1 {
				p.MShopPackRenewData[1] = int32(timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()))
			}
			if item.Id == 2 {
				p.MShopPackRenewData[2] = int32(timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()))
			}
			if item.Id == 3 {
				p.MShopPackRenewData[3] = int32(timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()) - int64(t.Weekday()*3600*24))
			}
		}
		p.Data.EnergyAdCount = MergeConst.G_Sta_Buy_Cnt
		p.EnergyRenewTime = int32(timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()))
		p.DailyRenewTime = int32(timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()))
		p.Data.MSpecialOfferBuyData[1] = 3
		p.Data.MSpecialOfferBuyData[2] = 3
		p.Data.MUISpecialOfferBuyData[1] = 3
		p.Data.MUISpecialOfferBuyData[2] = 3
		p.Data.MFreePackBuyData[1] = 1
		p.Data.MFreePackBuyData[2] = 1
		p.Data.MDiamondFirstBuyData[1] = 0
		p.Data.MDiamondFirstBuyData[2] = 0
		p.Data.MDiamondFirstBuyData[3] = 0
		p.Data.MDiamondFirstBuyData[4] = 0
		p.Data.MDiamondFirstBuyData[5] = 0
		p.Data.MDiamondFirstBuyData[6] = 0
	} else {

		var renewTime1 int64 = 0
		if t.Hour() >= 12 {
			renewTime1 = timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()) + 12*3600
		} else {
			renewTime1 = timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second())
		}

		if p.MsqlStruck.MShopTimeBuyData != "" {
			units := strings.Split(p.MsqlStruck.MShopTimeBuyData, "_")
			for i := 0; i < len(units); i++ {
				ss := strings.Split(units[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.Data.MShopTimeBuyData[int32(id)] = int32(value)
				if renewTime1 > int64(p.MsqlStruck.MItemRenewTimes) {
					item := ShopItems.Index(int(id)).(*gamedata.ShopItemRecord)
					p.Data.MShopTimeBuyData[int32(id)] = int32(item.TotalCount)
					p.MShopTimeRenew = int32(renewTime1)
				}
			}
		}
		LastDailyRenewTime := timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second())
		var isDailyRenew = false
		if p.MsqlStruck.MSpecialOfferBuyData != "" {
			units := strings.Split(p.MsqlStruck.MSpecialOfferBuyData, "_")
			for i := 0; i < len(units); i++ {
				ss := strings.Split(units[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.Data.MSpecialOfferBuyData[int32(id)] = int32(value)

				if LastDailyRenewTime > int64(p.MsqlStruck.DailyRenewTime) {
					isDailyRenew = true
					p.Data.MSpecialOfferBuyData[1] = 3
					p.Data.MSpecialOfferBuyData[2] = 3
				}
			}
		} else {
			isDailyRenew = true
			p.Data.MSpecialOfferBuyData[1] = 3
			p.Data.MSpecialOfferBuyData[2] = 3
		}
		if p.MsqlStruck.MUISpecialOfferBuyData != "" {
			units := strings.Split(p.MsqlStruck.MUISpecialOfferBuyData, "_")
			for i := 0; i < len(units); i++ {
				ss := strings.Split(units[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.Data.MUISpecialOfferBuyData[int32(id)] = int32(value)

				if LastDailyRenewTime > int64(p.MsqlStruck.DailyRenewTime) {
					isDailyRenew = true
					p.Data.MUISpecialOfferBuyData[1] = 3
					p.Data.MUISpecialOfferBuyData[2] = 3
				}
			}
		} else {
			isDailyRenew = true
			p.Data.MUISpecialOfferBuyData[1] = 3
			p.Data.MUISpecialOfferBuyData[2] = 3
		}
		if p.MsqlStruck.MFreePackBuyData != "" {
			units := strings.Split(p.MsqlStruck.MFreePackBuyData, "_")
			for i := 0; i < len(units); i++ {
				ss := strings.Split(units[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.Data.MFreePackBuyData[int32(id)] = int32(value)

				if LastDailyRenewTime > int64(p.MsqlStruck.DailyRenewTime) {
					isDailyRenew = true
					p.Data.MFreePackBuyData[1] = 1
					p.Data.MFreePackBuyData[2] = 1
				}
			}
		} else {
			isDailyRenew = true
			p.Data.MFreePackBuyData[1] = 1
			p.Data.MFreePackBuyData[2] = 1
		}
		if isDailyRenew {
			p.DailyRenewTime = int32(LastDailyRenewTime)
			p.MsqlStruck.DailyRenewTime = p.DailyRenewTime
		}

		if p.MsqlStruck.MDiamondFirstBuyData != "" {
			units := strings.Split(p.MsqlStruck.MDiamondFirstBuyData, "_")
			for i := 0; i < len(units); i++ {
				ss := strings.Split(units[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.Data.MDiamondFirstBuyData[int32(id)] = int32(value)
			}
		} else {
			p.Data.MDiamondFirstBuyData[1] = 0
			p.Data.MDiamondFirstBuyData[2] = 0
			p.Data.MDiamondFirstBuyData[3] = 0
			p.Data.MDiamondFirstBuyData[4] = 0
			p.Data.MDiamondFirstBuyData[5] = 0
			p.Data.MDiamondFirstBuyData[6] = 0
		}

		if p.MsqlStruck.MShopSaleBuyData != "" {
			unitsSale := strings.Split(p.MsqlStruck.MShopSaleBuyData, "_")
			for i := 0; i < len(unitsSale); i++ {
				ss := strings.Split(unitsSale[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.Data.MShopSaleBuyData[int32(id)] = int32(value)
				if renewTime1 > int64(p.MsqlStruck.MItemRenewTimes) {
					// item := ShopItems.Index(int(id)).(*gamedata.ShopItemRecord)
					// p.Data.MShopSaleBuyData[int32(id)] = int32(item.TotalCount)

					p.Data.MShopSaleBuyData[int32(id)] = 5
					p.MShopTimeRenew = int32(renewTime1)
				}
			}
		}
		if p.MsqlStruck.MPackRenewTimes != "" {
			unitsPackRenew := strings.Split(p.MsqlStruck.MPackRenewTimes, "_")
			for i := 0; i < len(unitsPackRenew); i++ {
				ss := strings.Split(unitsPackRenew[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.MShopPackRenewData[int32(id)] = int32(value)
			}
		}

		if p.MsqlStruck.MPackBuyData != "" {
			unitsPack := strings.Split(p.MsqlStruck.MPackBuyData, "_")
			var isRenew bool = false
			for i := 0; i < len(unitsPack); i++ {
				ss := strings.Split(unitsPack[i], ",")
				id, _ := strconv.ParseInt(ss[0], 10, 32)
				value, _ := strconv.ParseInt(ss[1], 10, 32)
				p.Data.MPackBuyData[int32(id)] = int32(value)
				t1, ok := p.MShopPackRenewData[int32(id)]
				if ok {
					if id == 1 {
						LastRenewTime := timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second())
						if t1 < int32(LastRenewTime) {
							item := PackItems.Index(int(id)).(*gamedata.ShopPackRecord)
							p.Data.MPackBuyData[int32(id)] = int32(item.TotalCount)
							p.MShopPackRenewData[int32(id)] = int32(LastRenewTime)
							isRenew = true
						}
					}
					if id == 2 {
						LastRenewTime := timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second())
						if t1 < int32(LastRenewTime) {
							item := PackItems.Index(int(id)).(*gamedata.ShopPackRecord)
							p.Data.MPackBuyData[int32(id)] = int32(item.TotalCount)
							p.MShopPackRenewData[int32(id)] = int32(LastRenewTime)
						}
					}
					if id == 3 {
						LastRenewTime := timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second()) - int64(t.Weekday()*3600*24)
						if t1 < int32(LastRenewTime) {
							item := PackItems.Index(int(id)).(*gamedata.ShopPackRecord)
							p.Data.MPackBuyData[int32(id)] = int32(item.TotalCount)
							p.MShopPackRenewData[int32(id)] = int32(LastRenewTime)
						}
					}
				}
			}

			p.Data.EnergyAdCount = p.MsqlStruck.EnergyAdCount

			p.EnergyRenewTime = p.MsqlStruck.EnergyRenewTime
			LastRenewTime := timeStamp - (int64)(t.Hour()*3600+t.Minute()*60+t.Second())
			if isRenew {
				p.Data.EnergyAdCount = MergeConst.G_Sta_Buy_Cnt
				p.EnergyRenewTime = int32(LastRenewTime)
			}
		}
	}
	p.Mcron = cron.New()

	p.Reconnect(false)
	return true
}
func (p *PlayerShopData) Reconnect(b bool) {
	p.ZeroCronID, _ = p.Mcron.AddFunc("0 0 * * *", func() {
		p.ZeroRenew()
	})
	p.WeekCronID, _ = p.Mcron.AddFunc("0 0 * * 0", func() {
		p.WeekRenew()
	})
	p.TwelveCronID, _ = p.Mcron.AddFunc("0 12 * * *", func() {
		p.TwelveRenew()
	})
	p.Mcron.Start()

}
func (p *PlayerShopData) ZeroRenew() {
	ShopItems := gamedata.GetConfigByName("ShopItem")
	PackItems := gamedata.GetConfigByName("ShopPack")
	timestamp := int32(time.Now().Unix())
	for k, _ := range p.Data.MShopTimeBuyData {
		item := ShopItems.Index(int(k)).(*gamedata.ShopItemRecord)
		p.Data.MShopTimeBuyData[k] = int32(item.TotalCount)
	}

	for k, _ := range p.Data.MShopSaleBuyData {
		// item := ShopItems.Index(int(k)).(*gamedata.ShopItemRecord)
		// p.Data.MShopSaleBuyData[k] = int32(item.TotalCount)
		p.Data.MShopSaleBuyData[k] = 5
	}
	p.MShopTimeRenew = timestamp
	for k, _ := range p.Data.MPackBuyData {
		if k == 1 || k == 2 {
			item := PackItems.Index(int(k)).(*gamedata.ShopPackRecord)
			p.Data.MPackBuyData[k] = int32(item.TotalCount)
			p.MShopPackRenewData[k] = timestamp
		}
	}
	p.EnergyRenewTime = timestamp
	p.DailyRenewTime = timestamp
	p.MsqlStruck.EnergyAdCount = MergeConst.G_Sta_Buy_Cnt
	p.Data.EnergyAdCount = p.MsqlStruck.EnergyAdCount

	p.Data.MSpecialOfferBuyData[1] = 3
	p.Data.MSpecialOfferBuyData[2] = 3

	p.Data.MUISpecialOfferBuyData[1] = 3
	p.Data.MUISpecialOfferBuyData[2] = 3

	p.Data.MFreePackBuyData[1] = 1
	p.Data.MFreePackBuyData[2] = 1

	p.NotifyShopStatusChange(1)
	p.NotifyShopStatusChange(2)
	p.NotifyShopStatusChange(3)
	p.NotifyShopStatusChange(4)
	p.NotifyShopStatusChange(5)
	p.NotifyShopStatusChange(6)
	p.NotifyShopStatusChange(7)
	p.SaveDataFromDB("")
}

func (p *PlayerShopData) TwelveRenew() {
	ShopItems := gamedata.GetConfigByName("ShopItem")
	timestamp := int32(time.Now().Unix())
	for k, _ := range p.Data.MShopTimeBuyData {
		item := ShopItems.Index(int(k)).(*gamedata.ShopItemRecord)
		p.Data.MShopTimeBuyData[k] = int32(item.TotalCount)
	}

	for k, _ := range p.Data.MShopSaleBuyData {
		// item := ShopItems.Index(int(k)).(*gamedata.ShopItemRecord)
		// p.Data.MShopSaleBuyData[k] = int32(item.TotalCount)
		p.Data.MShopSaleBuyData[k] = 5
	}
	p.MShopTimeRenew = timestamp
	p.NotifyShopStatusChange(1)
	p.NotifyShopStatusChange(2)
	p.SaveDataFromDB("")
}

func (p *PlayerShopData) WeekRenew() {

	PackItems := gamedata.GetConfigByName("ShopPack")
	timestamp := int32(time.Now().Unix())
	for k, _ := range p.Data.MPackBuyData {
		if k == 3 {
			item := PackItems.Index(int(k)).(*gamedata.ShopPackRecord)
			p.Data.MPackBuyData[k] = int32(item.TotalCount)
			p.MShopPackRenewData[k] = timestamp
		}
	}
	p.NotifyShopStatusChange(3)
	p.SaveDataFromDB("")
}

func (p *PlayerShopData) NotifyShopStatusChange(Type int32) {
	notify := &msg.NotifyShopStatusChange{}
	notify.MShopTimeBuyData = make(map[int32]int32)
	notify.Type = Type
	var temp map[int32]int32
	if Type == 4 {
		notify.MShopTimeBuyData[1] = p.Data.EnergyAdCount
	} else {
		if Type == 1 {
			temp = p.Data.MShopTimeBuyData
		}
		if Type == 2 {
			temp = p.Data.MShopSaleBuyData
		}
		if Type == 3 {
			temp = p.Data.MPackBuyData
		}
		if Type == 5 {
			temp = p.Data.MSpecialOfferBuyData
		}
		if Type == 6 {
			temp = p.Data.MUISpecialOfferBuyData
		}
		if Type == 7 {
			temp = p.Data.MFreePackBuyData
		}

		for k, v := range temp {
			notify.MShopTimeBuyData[k] = v
		}

	}
	notify.CurSvrTime = int32(time.Now().Unix())

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(notify)
	G_getGameLogic().PackResInfo(agent, "NotifyShopStatusChange", data)
}

func (p *PlayerShopData) ClearData() bool {
	p.SaveDataFromDB("")
	p.Mcron.Remove(p.ZeroCronID)
	p.Mcron.Remove(p.WeekCronID)
	p.Mcron.Remove(p.TwelveCronID)
	p.Mcron.Stop()
	return true
}
func (p *PlayerShopData) SaveDataFromDB(Key interface{}) bool {

	strarr := []string{}
	strarr1 := []string{}
	strarr2 := []string{}
	strarr3 := []string{}
	strarr4 := []string{}
	strarr5 := []string{}
	strarr6 := []string{}
	strarr7 := []string{}

	for k, v := range p.Data.MShopTimeBuyData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr = append(strarr, str)
	}
	str_concat := strings.Join(strarr, "_")
	p.MsqlStruck.MShopTimeBuyData = str_concat

	for k, v := range p.Data.MShopSaleBuyData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr1 = append(strarr1, str)
	}
	str_concat1 := strings.Join(strarr1, "_")
	p.MsqlStruck.MShopSaleBuyData = str_concat1

	for k, v := range p.Data.MPackBuyData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr2 = append(strarr2, str)
	}
	str_concat2 := strings.Join(strarr2, "_")
	p.MsqlStruck.MPackBuyData = str_concat2

	for k, v := range p.MShopPackRenewData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr3 = append(strarr3, str)
	}
	str_concat3 := strings.Join(strarr3, "_")
	p.MsqlStruck.MPackRenewTimes = str_concat3

	for k, v := range p.Data.MSpecialOfferBuyData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr4 = append(strarr4, str)
	}
	str_concat4 := strings.Join(strarr4, "_")
	p.MsqlStruck.MSpecialOfferBuyData = str_concat4

	for k, v := range p.Data.MUISpecialOfferBuyData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr5 = append(strarr5, str)
	}
	str_concat5 := strings.Join(strarr5, "_")
	p.MsqlStruck.MUISpecialOfferBuyData = str_concat5

	for k, v := range p.Data.MFreePackBuyData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr6 = append(strarr6, str)
	}
	str_concat6 := strings.Join(strarr6, "_")
	p.MsqlStruck.MFreePackBuyData = str_concat6

	for k, v := range p.Data.MDiamondFirstBuyData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr7 = append(strarr7, str)
	}
	str_concat7 := strings.Join(strarr7, "_")
	p.MsqlStruck.MDiamondFirstBuyData = str_concat7

	p.MsqlStruck.DwUin = p.M_Player.M_DwUin
	p.MsqlStruck.EnergyAdCount = p.Data.EnergyAdCount
	p.MsqlStruck.BuyEnergyTime = p.BuyEnergyTime
	p.MsqlStruck.EnergyRenewTime = p.EnergyRenewTime
	p.MsqlStruck.DailyRenewTime = p.DailyRenewTime
	p.MsqlStruck.MItemRenewTimes = p.MShopTimeRenew

	if p.IsHaveDataDb {
		db.FormatAllMemUpdateDb(&p.MsqlStruck, "t_player_shop_data", "dwUin")
	} else {
		db.FormatAllMemInsertDb(&p.MsqlStruck, "t_player_shop_data")
	}
	p.IsHaveDataDb = true
	return true
}

func (p *PlayerShopData) ResShopData(player *Player) {
	res := &msg.ResShopData{}
	res.MShopTimeBuyData = p.Data.MShopTimeBuyData
	res.MShopSaleBuyData = p.Data.MShopSaleBuyData
	res.MPackBuyData = p.Data.MPackBuyData
	res.EnergyAdCount = p.Data.EnergyAdCount
	res.LastEnergyBuyTime = p.BuyEnergyTime
	res.CurSvrTime = int32(time.Now().Unix())
	res.MSpecialOfferBuyData = p.Data.MSpecialOfferBuyData
	res.MUISpecialOfferBuyData = p.Data.MUISpecialOfferBuyData
	res.MFreePackBuyData = p.Data.MFreePackBuyData
	res.MDiamondFirstBuyData = p.Data.MDiamondFirstBuyData
	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResShopData", data)
}

func (p *PlayerShopData) ResRenewItemBuyCnt(buf []byte) {
	// ShopItems := gamedata.GetConfigByName("ShopItem")
	req := &msg.ReqRenewItemBuyCnt{}
	proto.Unmarshal(buf, req)
	res := &msg.ResRenewItemBuyCnt{}
	res.Type = req.Type
	res.MShopTimeBuyData = make(map[int32]int32)
	p.Data.MShopSaleBuyData = make(map[int32]int32)
	if req.Type == 2 {
		for k, v := range req.MShopData {
			p.Data.MShopSaleBuyData[k] = v
			res.MShopTimeBuyData[k] = v
		}

		// for k, _ := range p.Data.MShopSaleBuyData {
		// 	item := ShopItems.Index(int(k)).(*gamedata.ShopItemRecord)
		// 	p.Data.MShopSaleBuyData[k] = int32(item.TotalCount)
		// 	res.MShopTimeBuyData[k] = int32(item.TotalCount)
		// }
	}
	p.SaveDataFromDB("")
	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResRenewItemBuyCnt", data)
}

func (p *PlayerShopData) ResShopBuy(buf []byte) {
	req := &msg.ReqShopBuy{}
	proto.Unmarshal(buf, req)
	res := &msg.ResShopBuy{}
	res.ResultCode = 0
	res.MShopTimeBuyData = make(map[int32]int32)
	res.CurSvrTime = int32(time.Now().Unix())
	if req.Type == 1 {
		if p.Data.MShopTimeBuyData[req.BuyId] > 0 {
			p.Data.MShopTimeBuyData[req.BuyId] = p.Data.MShopTimeBuyData[req.BuyId] - 1
			res.MShopTimeBuyData[req.BuyId] = p.Data.MShopTimeBuyData[req.BuyId]
		} else {
			res.ResultCode = MergeConst.Protocol_Res_Buy_Cnt_Limit
		}

	}
	if req.Type == 2 {
		if p.Data.MShopSaleBuyData[req.BuyId] > 0 {
			p.Data.MShopSaleBuyData[req.BuyId] = p.Data.MShopSaleBuyData[req.BuyId] - 1
			// res.MShopTimeBuyData[req.BuyId] = p.Data.MShopSaleBuyData[req.BuyId]
			res.MShopTimeBuyData = p.Data.MShopSaleBuyData
		} else {
			res.ResultCode = MergeConst.Protocol_Res_Buy_Cnt_Limit
		}

	}
	if req.Type == 3 {
		if p.Data.MPackBuyData[req.BuyId] > 0 {
			p.Data.MPackBuyData[req.BuyId] = p.Data.MPackBuyData[req.BuyId] - 1
			res.MShopTimeBuyData[req.BuyId] = p.Data.MPackBuyData[req.BuyId]
		} else {
			res.ResultCode = MergeConst.Protocol_Res_Buy_Cnt_Limit
		}
	}

	if req.Type == 4 {
		deltaTime := res.CurSvrTime - p.BuyEnergyTime

		if p.Data.EnergyAdCount > 0 {
			if deltaTime >= MergeConst.G_Sta_Buy_CD {
				res.MShopTimeBuyData[req.BuyId] = p.Data.EnergyAdCount - 1
				p.Data.EnergyAdCount = p.Data.EnergyAdCount - 1
				p.BuyEnergyTime = res.CurSvrTime
			} else {
				res.ResultCode = MergeConst.Protocol_Res_Buy_CD
			}

		} else {
			res.ResultCode = MergeConst.Protocol_Res_Buy_Cnt_Limit
		}
	}
	if req.Type == 5 {
		if p.Data.MSpecialOfferBuyData[req.BuyId] > 0 {
			p.Data.MSpecialOfferBuyData[req.BuyId] = p.Data.MSpecialOfferBuyData[req.BuyId] - 1
			res.MShopTimeBuyData[req.BuyId] = p.Data.MSpecialOfferBuyData[req.BuyId]

		} else {
			res.ResultCode = MergeConst.Protocol_Res_Buy_Cnt_Limit
		}
	}
	if req.Type == 6 {
		if p.Data.MUISpecialOfferBuyData[req.BuyId] > 0 {
			p.Data.MUISpecialOfferBuyData[req.BuyId] = p.Data.MUISpecialOfferBuyData[req.BuyId] - 1
			res.MShopTimeBuyData[req.BuyId] = p.Data.MUISpecialOfferBuyData[req.BuyId]
		} else {
			res.ResultCode = MergeConst.Protocol_Res_Buy_Cnt_Limit
		}
	}
	if req.Type == 7 {
		if p.Data.MFreePackBuyData[req.BuyId] > 0 {
			p.Data.MFreePackBuyData[req.BuyId] = p.Data.MFreePackBuyData[req.BuyId] - 1
			res.MShopTimeBuyData[req.BuyId] = p.Data.MFreePackBuyData[req.BuyId]
		} else {
			res.ResultCode = MergeConst.Protocol_Res_Buy_Cnt_Limit
		}
	}

	if req.Type == 8 {
		p.Data.MDiamondFirstBuyData[req.BuyId] = 1
		res.MShopTimeBuyData[req.BuyId] = 1
	}
	res.Type = req.Type
	p.SaveDataFromDB("")
	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResShopBuy", data)
}

///////////////////////////////

type PlayerAdPackData struct {
	*PlayerData
	Data           msg.ResAdPackData
	SqlStruct      db.SqlAdPackStruct
	RenewDailyTime int32
	Turns          int
}

func (p *PlayerAdPackData) LoadDataFromDB(dwUin interface{}) bool {

	sqlStr := "SELECT * FROM t_player_AD_Pack WHERE dwUin = ?"

	// SqlStruct := db.SqlAdPackStruct{}
	p.Data.PackData = make(map[int32]int32)
	p.Data.RewardInfo = []string{}
	if err := db.SqlDb.Get(&p.SqlStruct, sqlStr, dwUin.(int32)); err != nil {
		fmt.Printf("get data failed, err:%v\n", err)
		p.IsHaveDataDb = false
	} else {
		p.IsHaveDataDb = true
	}
	p.CalcTurns()
	timeStamp := time.Now().Unix()
	t := time.Unix(timeStamp, 0).Local()
	p.RenewDailyTime = int32(timeStamp - int64(t.Hour())*3600 - int64(t.Minute())*60 - int64(t.Second()))
	if p.IsHaveDataDb == false {
		p.Data.PackData[1] = 0
		p.Data.PackData[2] = 0
		p.Data.PackData[3] = 0
		p.Data.PackData[4] = 0
		p.Data.PackData[5] = 0
		p.Data.PackData[6] = 0
		p.Data.Score = 0
		p.SqlStruct.CurTurns = 1
		p.SqlStruct.RenewDailyTime = p.RenewDailyTime
		p.RandomGenAdPackReward()
	} else {
		p.Data.Score = p.SqlStruct.Score

		if p.RenewDailyTime > p.SqlStruct.RenewDailyTime {
			p.Data.PackData[1] = 0
			p.Data.PackData[2] = 0
			p.Data.PackData[3] = 0
			p.Data.PackData[4] = 0
			p.Data.PackData[5] = 0
			p.Data.PackData[6] = 0
			p.Data.Score = 0
			p.SqlStruct.CurTurns = 1
			p.SqlStruct.RenewDailyTime = p.RenewDailyTime
			p.RandomGenAdPackReward()

		} else {
			if p.SqlStruct.PackData != "" {
				units := strings.Split(p.SqlStruct.PackData, "_")
				for i := 0; i < len(units); i++ {
					ss := strings.Split(units[i], ",")
					id, _ := strconv.ParseInt(ss[0], 10, 32)
					value, _ := strconv.ParseInt(ss[1], 10, 32)
					p.Data.PackData[int32(id)] = int32(value)
				}

			} else {
				p.Data.PackData[1] = 0
				p.Data.PackData[2] = 0
				p.Data.PackData[3] = 0
				p.Data.PackData[4] = 0
				p.Data.PackData[5] = 0
				p.Data.PackData[6] = 0
			}

			if p.SqlStruct.RewardInfo != "" {
				units := strings.Split(p.SqlStruct.RewardInfo, ";")
				for i := 0; i < len(units); i++ {
					p.Data.RewardInfo = append(p.Data.RewardInfo, units[i])
				}

			} else {
				p.RandomGenAdPackReward()
			}
		}

	}

	p.Reconnect(false)
	return true
}

func (p *PlayerAdPackData) CalcTurns() {
	lastAdCnt := p.GetPlayer().GetIFGameData("PlayerSingleData").(*PlayerSingleData).GetLastWatchAdCnt()
	if lastAdCnt <= 5 {
		p.Turns = 3
	} else {
		if lastAdCnt <= 20 {
			p.Turns = 4
		} else {
			p.Turns = 6
		}
	}
}

func (p *PlayerAdPackData) ClearData() bool {
	p.SaveDataFromDB("")
	GoUtil.RemoveEvent(MergeConst.Notify_Renew_Ad_Pack, p.Notify_Renew_Ad_Pack, p)
	return true
}

func (p *PlayerAdPackData) Reconnect(b bool) {
	GoUtil.RegisterEvent(MergeConst.Notify_Renew_Ad_Pack, p.Notify_Renew_Ad_Pack, p)
}
func (p *PlayerAdPackData) Notify_Renew_Ad_Pack(args []interface{}) {

	player := args[0].(*Player)
	if player != p.GetPlayer() {
		return
	}

	notify := &msg.NotifyAdPackData{}
	p.CalcTurns()
	p.Data.PackData[1] = 0
	p.Data.PackData[2] = 0
	p.Data.PackData[3] = 0
	p.Data.PackData[4] = 0
	p.Data.PackData[5] = 0
	p.Data.PackData[6] = 0
	p.Data.Score = 0
	p.SqlStruct.CurTurns = 1
	p.SqlStruct.RenewDailyTime = int32(G_GameLogicPtr.DailyTaskTimestamp)
	p.RandomGenAdPackReward()

	notify.PackData = p.Data.PackData
	notify.RewardInfo = p.Data.RewardInfo
	notify.Score = p.Data.Score
	notify.CurSvrTime = int32(time.Now().Unix())

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(notify)
	G_getGameLogic().PackResInfo(agent, "NotifyAdPackData", data)

}

func (p *PlayerAdPackData) RandomGenAdPackReward() {
	p.Data.RewardInfo = []string{}

	for i := 0; i < 3; i++ {
		ran := rand.Intn(100)
		if ran < 20 {
			str := "Energy_" + fmt.Sprintf("%d", 20)
			p.Data.RewardInfo = append(p.Data.RewardInfo, str)

		} else {
			if ran < 70 {
				str := "Energy_" + fmt.Sprintf("%d", 12)
				p.Data.RewardInfo = append(p.Data.RewardInfo, str)
			} else {
				str := "Energy_" + fmt.Sprintf("%d", 8)
				p.Data.RewardInfo = append(p.Data.RewardInfo, str)
			}
		}
	}
	for i := 0; i < 2; i++ {
		ran := rand.Intn(100)
		if ran < 20 {
			str := "Star_" + fmt.Sprintf("%d", 60)
			p.Data.RewardInfo = append(p.Data.RewardInfo, str)
		} else {
			if ran < 70 {
				str := "Star_" + fmt.Sprintf("%d", 40)
				p.Data.RewardInfo = append(p.Data.RewardInfo, str)
			} else {
				str := "Star_" + fmt.Sprintf("%d", 20)
				p.Data.RewardInfo = append(p.Data.RewardInfo, str)
			}
		}
	}
	for i := 0; i < 1; i++ {
		ran := rand.Intn(100)
		if ran < 20 {
			str := "Diamond_" + fmt.Sprintf("%d", 8)
			p.Data.RewardInfo = append(p.Data.RewardInfo, str)
		} else {
			if ran < 70 {
				str := "Diamond_" + fmt.Sprintf("%d", 5)
				p.Data.RewardInfo = append(p.Data.RewardInfo, str)
			} else {
				str := "Diamond_" + fmt.Sprintf("%d", 3)
				p.Data.RewardInfo = append(p.Data.RewardInfo, str)
			}
		}
	}
	disorder := G_GameLogicPtr.DisorderLuaTable(6, 6)
	tempList := []string{}
	for i := 0; i < len(disorder); i++ {
		temp := p.Data.RewardInfo[disorder[i]]
		tempList = append(tempList, temp)
	}
	p.Data.RewardInfo = tempList
}

func (p *PlayerAdPackData) SaveDataFromDB(Key interface{}) bool {
	strarr := []string{}
	for k, v := range p.Data.PackData {
		str := fmt.Sprintf("%d,%d", k, v)
		strarr = append(strarr, str)
	}
	str_concat := strings.Join(strarr, "_")

	p.SqlStruct.PackData = str_concat
	p.SqlStruct.RewardInfo = strings.Join(p.Data.RewardInfo, ";")
	p.SqlStruct.DwUin = p.M_Player.M_DwUin
	p.SqlStruct.Score = p.Data.Score
	if p.IsHaveDataDb {
		db.FormatAllMemUpdateDb(&p.SqlStruct, "t_player_AD_Pack", "dwUin")
	} else {
		db.FormatAllMemInsertDb(&p.SqlStruct, "t_player_AD_Pack")
	}
	p.IsHaveDataDb = true
	return true
}

func (p *PlayerAdPackData) ReqAdPackData(player *Player) {

	p.Data.CurSvrTime = int32(time.Now().Unix())
	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(&p.Data)
	G_getGameLogic().PackResInfo(agent, "ResAdPackData", data)

}

func (p *PlayerAdPackData) IsAllGetReward() bool {
	for _, v := range p.Data.PackData {
		if v == 0 {
			return false
		}
	}
	return true
}

func (p *PlayerAdPackData) ReqWatchAdPack(buf []byte) {
	req := &msg.ReqWatchAdPack{}
	res := &msg.ResWatchAdPack{}
	proto.Unmarshal(buf, req)

	if p.Data.PackData[req.ID] == 0 {
		p.Data.PackData[req.ID] = 1
		p.Data.Score = p.Data.Score + 1
		if p.IsAllGetReward() && p.SqlStruct.CurTurns < int32(p.Turns) {
			p.Data.PackData[1] = 0
			p.Data.PackData[2] = 0
			p.Data.PackData[3] = 0
			p.Data.PackData[4] = 0
			p.Data.PackData[5] = 0
			p.Data.PackData[6] = 0
			p.RandomGenAdPackReward()
			p.SqlStruct.CurTurns = p.SqlStruct.CurTurns + 1
			res.RewardInfo = p.Data.RewardInfo
			res.PackData = p.Data.PackData
			res.Score = p.Data.Score
			res.IsNew = 1
		} else {
			if p.IsAllGetReward() {
				p.SqlStruct.CurTurns = p.SqlStruct.CurTurns + 1
			}
			res.IsNew = 0
			res.RewardInfo = p.Data.RewardInfo
			res.PackData = p.Data.PackData
			res.Score = p.Data.Score
		}
	} else {
		res.ResultCode = MergeConst.Protocol_Ad_Pack_Geted
	}
	p.SaveDataFromDB("")

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResWatchAdPack", data)
}

/////////////////////////

type PlayerSingleData struct {
	*PlayerData
	Data       msg.ResPlayerSingleData
	MsqlStruck db.SqlSingleDataStruct
}

func (p *PlayerSingleData) LoadDataFromDB(dwUin interface{}) bool {

	sqlStr := "SELECT * FROM t_player_single_data WHERE dwUin = ?"

	if err := db.SqlDb.Get(&p.MsqlStruck, sqlStr, dwUin.(int32)); err != nil {
		fmt.Printf("get data failed, err:%v\n", err)
		p.IsHaveDataDb = false
	} else {
		p.IsHaveDataDb = true
	}

	timeStamp := time.Now().Unix()
	t := time.Unix(timeStamp, 0).Local()

	if p.IsHaveDataDb == false {

		p.Data.NewbiePackBuyID = 0
		p.Data.NewbiePackOpenTime = 0
		p.Data.NoAdCnt = 0
		p.Data.NoAdOpenSvrTime = 0
		p.Data.WatchAdCnt = 0
		p.Data.WatchAdOpenTime = 0
		p.Data.LastWatchAdCnt = 0
		p.Data.AllPayCnt = 0
	} else {
		p.Data.NewbiePackBuyID = p.MsqlStruck.NewbiePackBuyID
		p.Data.NewbiePackOpenTime = p.MsqlStruck.NewbiePackOpenTime
		p.Data.NoAdCnt = p.MsqlStruck.NoAdCnt
		p.Data.NoAdOpenSvrTime = p.MsqlStruck.NoAdOpenSvrTime
		p.Data.WatchAdCnt = p.MsqlStruck.WatchAdCnt
		p.Data.WatchAdOpenTime = p.MsqlStruck.WatchAdOpenTime
		p.Data.LastWatchAdCnt = p.MsqlStruck.LastWatchAdCnt

		AllPayCnt, _ := strconv.ParseFloat(p.MsqlStruck.AllPayCnt, 64)
		p.Data.AllPayCnt = float32(AllPayCnt)

		zero := int32(timeStamp - int64(t.Hour())*3600 - int64(t.Minute())*60 - int64(t.Second()))
		if zero > p.Data.NoAdOpenSvrTime {
			p.Data.NoAdCnt = 0
			p.Data.NoAdOpenSvrTime = zero
		}
		if zero > p.Data.WatchAdOpenTime {
			p.Data.LastWatchAdCnt = p.Data.WatchAdCnt
			p.Data.WatchAdCnt = 0
			p.Data.WatchAdOpenTime = zero
		}
	}

	p.Reconnect(false)
	return true
}
func (p *PlayerSingleData) Reconnect(b bool) {

	GoUtil.RegisterEvent(MergeConst.Notify_Daily_Renew, p.Notify_Daily_Renew, p)
}

func (p *PlayerSingleData) ReqPlayerSingleData(player *Player) {
	if player != p.M_Player {
		return
	}

	p.Data.CurSvrTime = int32(time.Now().Unix())
	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(&p.Data)
	G_getGameLogic().PackResInfo(agent, "ResPlayerSingleData", data)

}

func (p *PlayerSingleData) ReqOpenNewbiePack(buf []byte) {
	req := &msg.ReqOpenNewbiePack{}
	res := &msg.ResOpenNewbiePack{}
	proto.Unmarshal(buf, req)

	if p.Data.NewbiePackOpenTime > 0 {
		res.ResultCode = MergeConst.Protocol_Newbie_Pack_Opened
	} else {
		res.ResultCode = 0
		p.Data.NewbiePackOpenTime = int32(time.Now().Unix())
		p.Data.NewbiePackBuyID = 0
		res.NewbiePackOpenTime = p.Data.NewbiePackOpenTime
		res.NewbiePackBuyID = 0
	}
	p.SaveDataFromDB("")

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResOpenNewbiePack", data)
}

func (p *PlayerSingleData) ReqBuyNewbiePack(buf []byte) {
	req := &msg.ReqBuyNewbiePack{}
	res := &msg.ResBuyNewbiePack{}
	proto.Unmarshal(buf, req)
	cur := int32(time.Now().Unix())

	if p.Data.NewbiePackOpenTime+3600*24 < cur {
		res.ResultCode = MergeConst.Protocol_Newbie_Pack_OutLine
	} else {
		res.ResultCode = 0
		p.Data.NewbiePackBuyID = req.NewbiePackBuyID
		res.NewbiePackBuyID = req.NewbiePackBuyID
	}
	p.SaveDataFromDB("")

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResBuyNewbiePack", data)
}

func (p *PlayerSingleData) ReqAddNoAdCnt(buf []byte) {
	req := &msg.ReqAddNoAdCnt{}
	res := &msg.ResAddNoAdCnt{}
	proto.Unmarshal(buf, req)
	res.NoAdCnt = p.Data.NoAdCnt + 1
	p.Data.NoAdCnt = res.NoAdCnt

	p.SaveDataFromDB("")

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResAddNoAdCnt", data)
}

func (p *PlayerSingleData) AddAllPayCnt(add float32) {
	p.Data.AllPayCnt = p.Data.AllPayCnt + add
	p.SaveDataFromDB("")
}

func (p *PlayerSingleData) GetLastWatchAdCnt() int32 {
	return p.Data.LastWatchAdCnt
}

func (p *PlayerSingleData) ReqAddWatchAdCnt(buf []byte) {
	req := &msg.ReqAddWatchAdCnt{}
	res := &msg.ResAddWatchAdCnt{}
	proto.Unmarshal(buf, req)
	res.WatchAdCnt = p.Data.WatchAdCnt + 1
	p.Data.WatchAdCnt = res.WatchAdCnt

	p.SaveDataFromDB("")

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResAddWatchAdCnt", data)
}

func (p *PlayerSingleData) Notify_Daily_Renew(param []interface{}) {
	zero := int32(G_GameLogicPtr.DailyTaskTimestamp)
	p.Data.NoAdCnt = 0
	p.Data.NoAdOpenSvrTime = zero
	p.Data.LastWatchAdCnt = p.Data.WatchAdCnt
	p.Data.WatchAdCnt = 0
	p.Data.WatchAdOpenTime = zero

	notify := &msg.NotifyPlayerSingleData{}
	notify.NewbiePackBuyID = p.Data.NewbiePackBuyID
	notify.NewbiePackOpenTime = p.Data.NewbiePackOpenTime
	notify.NoAdCnt = p.Data.NoAdCnt
	notify.NoAdOpenSvrTime = p.Data.NoAdOpenSvrTime
	notify.WatchAdCnt = p.Data.WatchAdCnt
	notify.WatchAdOpenTime = p.Data.WatchAdOpenTime
	notify.LastWatchAdCnt = p.Data.LastWatchAdCnt
	notify.AllPayCnt = p.Data.AllPayCnt
	notify.CurSvrTime = int32(time.Now().Unix())

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(notify)
	G_getGameLogic().PackResInfo(agent, "NotifyPlayerSingleData", data)
	p.SaveDataFromDB("")
	GoUtil.CallEvent(MergeConst.Notify_Renew_Ad_Pack, []interface{}{p.GetPlayer()})
}

func (p *PlayerSingleData) ClearData() bool {
	p.SaveDataFromDB("")
	GoUtil.RemoveEvent(MergeConst.Notify_Daily_Renew, p.Notify_Daily_Renew, p)
	return true
}

func (p *PlayerSingleData) SaveDataFromDB(Key interface{}) bool {
	p.MsqlStruck.NewbiePackBuyID = p.Data.NewbiePackBuyID
	p.MsqlStruck.NewbiePackOpenTime = p.Data.NewbiePackOpenTime
	p.MsqlStruck.NoAdCnt = p.Data.NoAdCnt
	p.MsqlStruck.NoAdOpenSvrTime = p.Data.NoAdOpenSvrTime
	p.MsqlStruck.WatchAdCnt = p.Data.WatchAdCnt
	p.MsqlStruck.WatchAdOpenTime = p.Data.WatchAdOpenTime
	p.MsqlStruck.LastWatchAdCnt = p.Data.LastWatchAdCnt
	p.MsqlStruck.AllPayCnt = fmt.Sprintf("%f", p.Data.AllPayCnt)
	p.MsqlStruck.DwUin = p.M_Player.M_DwUin
	if p.IsHaveDataDb {
		db.FormatAllMemUpdateDb(&p.MsqlStruck, "t_player_single_data", "dwUin")
	} else {
		db.FormatAllMemInsertDb(&p.MsqlStruck, "t_player_single_data")
	}
	p.IsHaveDataDb = true
	return true
}

/////////////////////////////////////////////

type PlayerChargeData struct {
	*PlayerData
	Data           msg.ResPlayerPayData
	SqlStruct      []db.SqlChargeStruct
	RenewDailyTime int32
	Turns          int
}

func (p *PlayerChargeData) LoadDataFromDB(dwUin interface{}) bool {

	sqlStr := "SELECT * FROM t_player_Charge_Data WHERE dwUin = ?"

	p.Data.PlayerPayData = []*msg.PlayerPayItem{}
	if err := db.SqlDb.Select(&p.SqlStruct, sqlStr, dwUin.(int32)); err != nil {
		fmt.Printf("get data failed, err:%v\n", err)
		p.IsHaveDataDb = false
	} else {
		p.IsHaveDataDb = true
	}

	if p.IsHaveDataDb == false {

	} else {

		for i := 0; i < len(p.SqlStruct); i++ {
			item := &msg.PlayerPayItem{}
			item.PayTime = p.SqlStruct[i].PayTime
			v, _ := strconv.ParseFloat(p.SqlStruct[i].PayValue, 64)
			item.PayValue = float32(v)
			item.AutoId = p.SqlStruct[i].Autoid
			p.Data.PlayerPayData = append(p.Data.PlayerPayData, item)
		}
		deleteList := p.GetNeedDeleteList()
		p.DeleteOutlinePay(deleteList, "t_player_Charge_Data")
	}
	p.Reconnect(false)
	return true
}

func (p *PlayerChargeData) GetNeedDeleteList() []int32 {
	deleteList := []int32{}
	for i := 0; i < len(p.Data.PlayerPayData); i++ {
		if p.Data.PlayerPayData[i].PayTime+35*3600*24 < int32(time.Now().Unix()) {
			deleteList = append(deleteList, p.Data.PlayerPayData[i].AutoId)
		}
	}
	return deleteList
}

func (p *PlayerChargeData) DeleteOutlinePay(deleteList []int32, tableName string) {
	sqlStr := "DELETE FROM " + tableName + " WHERE auto_id = ?"
	for i := 0; i < len(deleteList); i++ {

		result, err := db.SqlDb.Exec(sqlStr, deleteList[i])
		if err != nil {
			fmt.Printf("exec failed, err:%v\n", err)
			return
		}
		affectedRows, err := result.RowsAffected()
		if err != nil {
			fmt.Printf("get affected failed, err:%v\n", err)
			return
		}
		fmt.Printf("delete data success, affected rows:%d\n", affectedRows)
	}
}

func (p *PlayerChargeData) ClearData() bool {
	p.SaveDataFromDB("")

	return true
}

func (p *PlayerChargeData) Reconnect(b bool) {

}

func (p *PlayerChargeData) SaveDataFromDB(Key interface{}) bool {

	p.IsHaveDataDb = true
	return true
}

func (p *PlayerChargeData) ReqPlayerPayData(player *Player) {
	deleteList := p.GetNeedDeleteList()
	p.DeleteOutlinePay(deleteList, "t_player_Charge_Data")

	p.Data.CurSvrTime = int32(time.Now().Unix())
	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(&p.Data)
	G_getGameLogic().PackResInfo(agent, "ResPlayerPayData", data)

}

func (p *PlayerChargeData) ReqAddPay(buf []byte) {
	req := &msg.ReqAddPay{}
	res := &msg.ResAddPay{}
	proto.Unmarshal(buf, req)

	item := db.SqlChargeStruct{}
	item.PayTime = int32(time.Now().Unix())
	item.PayValue = fmt.Sprintf("%f", req.Add)
	item.DwUin = p.GetPlayer().M_DwUin

	msgItem := &msg.PlayerPayItem{}
	msgItem.PayTime = item.PayTime
	msgItem.PayValue = req.Add

	res.Add = req.Add
	InsertId, err := db.FormatAllMemInsertDb(&item, "t_player_Charge_Data")
	if err != nil {

	} else {
		msgItem.AutoId = int32(InsertId)
		p.Data.PlayerPayData = append(p.Data.PlayerPayData, msgItem)
		p.GetPlayer().GetIFGameData("PlayerSingleData").(*PlayerSingleData).AddAllPayCnt(req.Add)
	}
	p.SaveDataFromDB("")

	agent := p.GetPlayer().GetAgentByPlayer()
	data, _ := proto.Marshal(res)
	G_getGameLogic().PackResInfo(agent, "ResAddPay", data)
}
